package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmUserDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    /**
     * 自媒体添加用户
     * @param wmUser
     * @return
     */
    @Transactional
    @Override
    public ResponseResult seveWmUser(WmUser wmUser) {

        //查询用户是否已经存在
        LambdaQueryWrapper<WmUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WmUser::getApUserId, wmUser.getApUserId());
        WmUser one = getOne(wrapper);
        if (null != one) {
            return ResponseResult.okResult(one.getId());
        }

        //储存用户信息
        boolean save = save(wmUser);

        //获取作者id
        WmUser author = getOne(wrapper);

        //判断是否保存成功
        if (save) {
            //保存成功返回作者id
            return ResponseResult.okResult(author.getId());
        }

        //保存失败返回错误
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
    }

    /**
     * 登录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(WmUserDto dto) {

        //校验参数是否存在
        if (StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //获取数据库中的user对象
        LambdaQueryWrapper<WmUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WmUser::getName, dto.getName());
        WmUser wmUser = getOne(wrapper);

        //判断账号是否存在
        if (null == wmUser) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "账号不存在");
        }

        //将参数密码经行加密判断
        String word = dto.getPassword() + wmUser.getSalt();
        String wordSalt = DigestUtils.md5DigestAsHex(word.getBytes());

        //判断密码是否正确
        if (wordSalt.equals(wmUser.getPassword())) {

            //正确返回token
            String token = AppJwtUtil.getToken(Long.valueOf(wmUser.getId()));
            Map<String, Object> map = new HashMap<>();
            wmUser.setPassword("");
            wmUser.setSalt("");
            map.put("token", token);
            map.put("user", wmUser);
            return ResponseResult.okResult(map);
        } else {

            //错误 返回错误参数
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
    }
}
